//
// Created by Administrator on 2021/10/20.
//
#include "doublyList.h"
#include <iostream>

using namespace std;

void DoublyList::testAll() const
{
    Node *temp = first;
    while (temp != nullptr)
    {
        cout << temp->getData() << " ";
        temp = temp->getNext();
    }
    cout << endl;
}

void DoublyList::createAList()
{
    // section 1
    first = new Node(2, nullptr, nullptr);
    last = first;
    count = 1;
//    cout << "section 1 and count = " << count << endl;
//    testAll();
    // section 2
    first->setPrev(new Node(3, nullptr, first));
    first = first->getPrev();
    count++;
//    cout << "section 2 and count = " << count << endl;
//    testAll();
    // section 3
    Node* pNode = new Node(4, first, last);
    first->setNext(pNode);
    last->setPrev(pNode);
    count++;
//    cout << "section 3 and count = " << count << endl;
//    testAll();
    // section 4
    first = last->getPrev();
    delete first->getPrev();
    first->setPrev(nullptr);
    count--;
//    cout << "section 4 and count = " << count << endl;
//    testAll();
    // section 5
    last->setNext(new Node(5, last, nullptr));
    last = last->getNext();
    last->setNext(new Node(6, last, nullptr));
    last = last->getNext();
    last->setNext(new Node(7, last, nullptr));
    last = last->getNext();
    count += 3;
//    cout << "section 5 and count = " << count << endl;
//    testAll();
    // section 6    4 2 5 6 7 -> 4 7 2 5 6
    last = last->getPrev();
    last->getNext()->setNext(first->getNext());
    last->getNext()->getNext()->setPrev(last->getNext());
    last->getNext()->setPrev(first);
    first->setNext(last->getNext());
    last->setNext(nullptr);
//    cout << "section 6 and count = " << count << endl;
//    testAll();
    // section 7    4 7 2 5 6 -> 7 2 5 4 6
    first = first->getNext();
    first->getPrev()->setNext(last);
    first->getPrev()->setPrev(last->getPrev());
    last->getPrev()->setNext(first->getPrev());
    last->setPrev(first->getPrev());
    first->setPrev(nullptr);
//    cout << "section 7 and count = " << count << endl;
//    testAll();
    // section 8    7 2 5 4 6 -> 2 4 5 6 7
    int temp = first->getData();
    first->setData(first->getNext()->getData());
    first->getNext()->setData(last->getPrev()->getData());
    last->getPrev()->setData(last->getData());
    last->setData(temp);
//    cout << "section 8 and count = " << count << endl;
//    testAll();
    // section 9    2 4 5 6 7 ->  1 2 3 4 5 6 7
    first->setPrev(new Node(1, nullptr, first));
    first->getNext()->setPrev(new Node(3, first, first->getNext()));
    first->setNext(first->getNext()->getPrev());
    first = first->getPrev();
    count += 2;
//    cout << "section 9 and count = " << count << endl;
//    testAll();

}




